package com.wwwc.index.web.servlet;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.text.DecimalFormat;

import javax.servlet.ServletRequestWrapper;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class ScheduleDServlet extends HttpServlet {
    DecimalFormat td = new DecimalFormat("0.00");
    EJBDatabaseQuery ejbdb;

    String addCallBack       = "1-1";
    String viewCallBack      = "1-2";
    String managerCallBack   = "3-1";
    String adminCallBack     = "4-1";

    int file_size_max     = 50;
    ServletBean _sBean    = null; 
    ScheduleD sBean       = null;
    ScheduleDViewer vBean = null;
    ScheduleDUpdate uBean = null;
    ScheduleDAdmin  aBean = null;
    ScheduleDData   dBean = null;
    int sw = 1280;
    int sh = 1024; 
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	try {
	    boolean debug = false;
	    response.setContentType("text/html");
	    PrintWriter out     = response.getWriter();
	    String user_ip      = request.getRemoteAddr();
	    HttpSession session = request.getSession(true);
	    String call_path    = request.getContextPath()+request.getServletPath();

	    String mid    = (String)request.getAttribute("mid");
	    if(mid == null) {
		System.out.println("ScheduleDDataLoader:Error:101");
		return;
	    }
	    
	    String action  = request.getParameter("aid");
	    String sub_dir = request.getParameter("sub_dir");

	    Hashtable achs = (Hashtable)request.getAttribute("achs");
	    if(achs == null) {
		return;
	    }

	    int read_level     = Integer.parseInt((String)achs.get("read_level"));
	    int read_age       = Integer.parseInt((String)achs.get("read_age"));
	    int post_level     = Integer.parseInt((String)achs.get("post_level"));
	    int post_age       = Integer.parseInt((String)achs.get("post_age"));

	    boolean manager  = (Boolean.valueOf((String)achs.get("show_manager"))).booleanValue();
	    boolean admin    = (Boolean.valueOf((String)achs.get("show_admin"))).booleanValue();

	    //===================================================================
	    // user info
	    //===================================================================

	    String user_name    = null;
	    int user_age        = 0;
	    int user_level      = 0;
	    String user_fname   = null;
	    String user_since   = null;
	    String user_edu     = null;
	    String user_zip     = null;
	    String user_sex     = null;
	    String screen_w     = null;
	    String screen_h     = null;

	    Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	    if(user_info == null) {
		System.out.println("ScheduleDDataLoader:Error:102");
		return;
	    }

	    user_name     = (String)user_info.get("user_name");
	    user_age      = Integer.parseInt((String)user_info.get("user_age"));
	    user_level    = Integer.parseInt((String)user_info.get("user_level"));
	    user_fname    = (String)user_info.get("user_fname");
	    user_since    = (String)user_info.get("user_since");
	    user_edu      = (String)user_info.get("user_edu");
	    user_zip      = (String)user_info.get("user_zip");
	    user_sex      = (String)user_info.get("user_sex");
	    screen_w      = (String)user_info.get("screen_w");
	    screen_h      = (String)user_info.get("screen_h");

	    sw = Integer.parseInt(screen_w);
	    sh = Integer.parseInt(screen_h);

	    String conf_file_size_max = request.getParameter("file_size_max");
	    if(conf_file_size_max != null) {
		file_size_max = Integer.parseInt(conf_file_size_max);
	    }

	    //out.println(DebugBean.getInputParameters("ScheduleDServlet",request));
	    _sBean = new ServletBean();
	    out.println(_sBean.getSubdirInfo(request));
	    _sBean = null;

	    sBean = new ScheduleD();
	    out.println(sBean.getOptionBar(call_path, mid, addCallBack, viewCallBack, adminCallBack, managerCallBack,
					   user_name, user_level, manager, admin));
	    sBean = null;
	    
	    if(action == null) {
		if(user_level>0) {
		    if(vBean == null) {
			vBean = new ScheduleDViewer();
		    }
		    out.println(vBean.main(request, call_path, mid, user_name, user_level, manager, admin));
		    vBean = null;
		}
		else {
		    dBean = new ScheduleDData(sw,sh);
		    out.println(dBean.addScheduleDData(request, call_path, mid,  user_name, user_level, manager, admin));
		    dBean = null;
		}
	    }

	    if(action != null && action.equals("1-1")) {
		dBean = new ScheduleDData(sw,sh);
		out.println(dBean.addScheduleDData(request, call_path, mid,  user_name, user_level, manager, admin));
		dBean = null;
	    }

	    if(action != null &&  action.equals("1-2")) {
		if(vBean == null) {
		    vBean = new ScheduleDViewer();
		}
		out.println(vBean.main(request, call_path, mid, user_name, user_level, manager, admin));
		vBean = null;
	    }

	    if(action != null &&  action.equals("3-1")) {
		out.println("Manager");
	    }

	    if(action != null &&  action.equals("4-1")) {
		aBean = new ScheduleDAdmin();
		out.println(aBean.main(request, call_path, mid));
		aBean = null;
	    }

	    _sBean = null;
	    sBean = null;
	    aBean = null;
	    vBean = null;
	    dBean = null;

	}
	catch (Exception e) {
	    System.out.println("ScheduleDServlet:Error:401:"+e);
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
	throws IOException, ServletException {
	doPost(request, response);
    }
}









